<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-row class="search-input">
          <el-form-item prop="variableName" label="变量名：">
            <el-select v-model="filters.variableName" placeholder="变量名">
              <el-option
                v-for="item in variableNameOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="operator" label="变量符号：">
            <el-select v-model="filters.operator" placeholder="变量符号">
              <el-option
                v-for="item in operatorOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button label="批量停用" type="primary" @click="batchStatusOrderExamine"/>
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh"
              :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <el-row class="t_r">
      <el-button-group>
        <el-tooltip content="刷新" placement="top">
          <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
        </el-tooltip>
        <el-tooltip content="列显示" placement="top">
          <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
        </el-tooltip>
      </el-button-group>
    </el-row>
    <div class="toolbar" style="float:right;padding-right:15px;">
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :highlightCurrentRow="true"
      :content="pageResult"
      :totalSize="totalSize"
      :columns="filterColumns"
      :showSelection="true"
      :showBatchDelete="false"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @selectionChange="selectionChange"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation ? '新增' : '编辑'"
      width="30%"
      :visible.sync="editDialogVisibleMatching"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataFormMatching"
        label-width="100px"
        ref="dataFormMatching"
        :size="size"
        :rules="dataFormRulesMatching"
      >
        <el-row class="searchDialog-input">
          <el-form-item label="id：" prop="id" v-if="false">
            <el-input
              v-model="dataFormMatching.id"
              :disabled="true"
              auto-complete="off"
              placeholder="请输入id"
            ></el-input>
          </el-form-item>
          <el-form-item label="变量名：" prop="variableName">
            <el-select
              v-model="dataFormMatching.variableName"
              placeholder="变量名"
              style="width: 100%"
            >
              <el-option
                v-for="item in variableNameOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运算符：" prop="operator">
            <el-select v-model="dataFormMatching.operator" placeholder="运算符" style="width: 100%">
              <el-option
                v-for="item in operatorOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="条件值：" prop="conditionValue">
            <el-input
              v-model="dataFormMatching.conditionValue"
              auto-complete="off"
              placeholder="请输入条件值"
              oninput="value=value.replace(/[^\d]/g,'')"
              type="number"
            ></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click="editDialogVisibleMatching = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitFormMatching"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import CyTable from "@/views/Core/ProductTable";
import { param } from "../../../utils";

export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      variableNameOptions: [
        { key: "1", value: "收货省份" },
        { key: "2", value: "收货城市" },
        { key: "3", value: "订单金额" },
        { key: "4", value: "单商品数量" },
        { key: "5", value: "收货人姓名" },
        { key: "6", value: "订单备注" }
      ],
      operatorOptions: [
        { key: "1", value: "=" },
        { key: "2", value: "!=" },
        { key: "3", value: ">" },
        { key: "4", value: "<" },
        { key: "5", value: "like" },
        { key: "6", value: "is not null" }
      ],
      size: "small",
      filters: {
        variableName: "",
        operator: ""
      },
      columns: [
        {
          prop: "variableName",
          label: "变量名",
          minWidth: 120,
          formatter: this.formateVariableName
        },
        {
          prop: "operator",
          label: "变量符号",
          minWidth: 120,
          formatter: this.formateOperator
        },
        { prop: "conditionValue", label: "变量值", minWidth: 120 },
        { prop: "status", label: "状态", minWidth: 120, formatter: this.statusFormate},
        { prop: "createTime", label: "创建时间", minWidth: 120 }
      ],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: [],
      totalSize: 0,
      operation: false, // true:新增, false:编辑
      editDialogVisible: false, // 新增编辑界面是否显示
      editDialogVisibleMatching: false,
      editLoading: false,
      dataFormRulesMatching: {
        variableName: [
          { required: true, message: "请选择变量名", trigger: "change" }
        ],
        operator: [
          { required: true, message: "请选择变量符号", trigger: "change" }
        ],
        conditionValue: [
          { required: true, message: "请选择条件值", trigger: "change" }
        ]
      },

      dataFormMatching: {
        id: "",
        conditionValue: "",
        operator: "",
        variableName: ""
      }
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(params) {
      let this_ = this;
      if (params == undefined || params == null) {
        params = {
          pageNum: this_.pageRequest.pageNum,
          pageSize: this_.pageRequest.pageSize
        };
      }
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = params.pageNum;
      this_.filters.limit = params.pageSize;
      this.utils.request.queryOrderExamine(this_.filters, function(res) {
        if (res.data.rows == null) {
          res.data.rows = [];
        }
        this_.pageResult = res.data.rows;
        this_.totalSize = Number(res.data.total);
      });
    },
    handleAdd: function() {
      this.operation = true;
      this.editDialogVisibleMatching = true;
      (this.dataFormMatching = {
        id: "",
        conditionValue: "",
        operator: "",
        variableName: ""
      }),
        this.$nextTick(() => {
          this.$refs["dataFormMatching"].clearValidate();
        });
    },
    //选择框
    selectionChange: function(params) {
      this.checked = params.selections;
    },
    // 批量删除
    handleDelete: function(data) {
      let param = {};
      param.id = data.params[0].id;
      this.utils.request.deleteOrderExamine(param, this.deletInfoBack);
    },
    // 删除修改回调
    deletInfoBack: function(data) {
      if (data.code == "0000") {
        this.$message({ message: "操作成功", type: "success" });
      } else {
        this.$message({ message: "操作失败, " + data.msg, type: "error" });
      }
      this.findPage();
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.operation = false;
      this.editDialogVisibleMatching = true;
      this.dataFormMatching = {
        id: params.row.id,
        conditionValue: params.row.conditionValue,
        operator: params.row.operator,
        variableName: params.row.variableName
      };
      this.$nextTick(() => {
        this.$refs["dataFormMatching"].clearValidate();
      });
    },
    // 编辑
    submitFormMatching: function() {
      let this_ = this;
      this.$refs.dataFormMatching.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            let params = Object.assign({}, this.dataFormMatching);
            params.status = 1;
            this.utils.request.editOrderExamine(params, function(res) {
              if (res.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });
                this_.editDialogVisibleMatching = false;
                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          });
        }
      });
    },
    batchStatusOrderExamine(){
      let that = this; 
      let selections = that.checked;
      if(selections.length==0){
        this.$message({ message: "请选择要操作的数据",   type: "warning"});
        return false;
      }
      //判断状态是否为待提交
      for(var i =0; i < selections.length; i++){
        if(selections[i].status != 1){
          that.$message({ message: "当前数据勾选状态有误",   type: "warning"});
          return false;
        }
      }
      this.utils.request.batchStatusOrderExamine({"status":2,"orderExamineRequestList":selections} , res => {
        if(res.code == "0000"){
          that.$message({ message: "操作成功",   type: "success"});
          that.findPage();
        }else if(res.code == "9999") {
          that.$message({ message:res.msg,   type: "error"});
        }
      });
    },
    resetForm(formName) {
      this.filters = {
        variableName: "",
        operator: ""
      };
      this.findPage();
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      let temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    formateVariableName(row, column, cellValue, index) {
      if (cellValue == "1") {
        return "收货省份";
      } else if (cellValue == "2") {
        return "收货城市";
      } else if (cellValue == "3") {
        return "订单金额";
      } else if (cellValue == "4") {
        return "单商品数量";
      } else if (cellValue == "5") {
        return "收货人姓名";
      } else {
        return "订单备注";
      }
    },
    formateOperator(row, column, cellValue, index) {
      if (cellValue == "1") {
        return "=";
      } else if (cellValue == "2") {
        return "!=";
      } else if (cellValue == "3") {
        return ">";
      } else if (cellValue == "4") {
        return "<";
      } else if (cellValue == "5") {
        return "like";
      } else {
        return "is not null";
      }
    },
    statusFormate(row, column, cellValue, index) {
      if (cellValue == "1") {
        return "启用";
      } else if (cellValue == "2") {
        return "停用";
      }
    }
  },
  mounted() {
    this.initColumns();
  }
};
</script>

<style lang='scss' scoped>
</style>
